package weather

import (
	"gin-base/base/resp"
	"gin-base/common/services/weather"
	"github.com/gin-gonic/gin"
	"regexp"
)

func Index(this *gin.Context) {
	location, _ := this.GetQuery("location")
	if location == "" {
		reg := regexp.MustCompile(`(\d+)\.(\d+)\.(\d+)\.(\d+)`)

		ip := reg.FindString(this.Request.RemoteAddr)
		if ip != "" {
			location = ip
		} else {
			location = "北京"
		}
	}

	nowAirData := weather.GetNowAirData(location)
	nowLifeData := weather.GetNowLifeData(location)
	nowWeatherData := weather.GetNowWeatherData(location)

	data := make(map[string]map[string]interface{})

	data["air"] = make(map[string]interface{})
	data["life"] = make(map[string]interface{})
	data["weather"] = make(map[string]interface{})

	data["life"]["sport"] = nowLifeData["sport"]

	data["weather"]["text"] = nowWeatherData["text"]
	data["weather"]["code"] = nowWeatherData["code"]
	data["weather"]["temperature"] = nowWeatherData["temperature"]
	data["weather"]["wind_scale"] = nowWeatherData["wind_scale"]

	data["air"]["pm25"] = nowAirData["pm25"]

	resp.Success(this, data)
}
